var setTemp = new Set();
var setTempobject = [];
//var commmonSelectSetting = JSON.parse(sessionStorage.getItem("commmonSelectSetting"));
var commmonSelectSetting = top.commmonSelectSetting;
var selectType=commmonSelectSetting.selectType;
$(function () {
	var ids=commmonSelectSetting.ids;
	var grid_selector="#jqGrid";
	if (ids) {//如果有默认勾选的值，就要默认勾选
		var idsArray=ids.split(",");
		$.each(idsArray,function(i,item){
         saveData(item);
      })
	}
	var multiboxonlyParam=false;
	var jqGridSetting={
			url: commmonSelectSetting.url,
	        datatype: "json",
	        colModel:commmonSelectSetting.colModel,
			viewrecords: true,
	        height: 385,
	        rowNum: 10,
			rowList : [10,30,50],
	        rownumbers: true, 
	        rownumWidth: 25, 
	        autowidth:true,
	        multiselect: true,  
	        postData:commmonSelectSetting.postData,
	        //multiboxonly:true,
	        beforeSelectRow: function(){//js方法  
	        	if(selectType=='radio'){  
	        		$("#jqGrid").jqGrid('resetSelection');  
	        	}
	            return true;  
	        },
	        onSelectAll:function(aRowids,status){
	            if(status==true){
	                //循环aRowids数组，将code放入arrayNewList数组中
	                $.each(aRowids,function(i,item){
	                   saveData(item);
	                })
	            }else{
	                //循环aRowids数组，将code从arrayNewList中删除
	                $.each(aRowids,function(i,item){
	                   deleteIndexData(item);
	                })
	            }

	        },
	        onSelectRow:function(aRowids,status){
	            if(status==true){
	               saveData(aRowids);
	            }else{
	               deleteIndexData(aRowids);
	            }

	        },
	        loadComplete:function(xhr){
        		var array = xhr.page.list;
 	            if (setTemp.size > 0) {
 	            	var itemIdname=commmonSelectSetting.colModel[0]['name'];//数据的主键的名字
 	                $.each(array, function (i, item) {
 	                	var idtemp=item[itemIdname];
 	                    if (setTemp.has(idtemp+"")) {
 	                    	$("#jqg_jqGrid_" + idtemp).attr("checked", true);
 	                    }
 	                });
 	            }
 	           setTempobject = setTempobject.concat(array);
 	           
	        },
	        pager: "#jqGridPager",
	        jsonReader : {
	            root: "page.list",
	            page: "page.currPage",
	            total: "page.totalPage",
	            records: "page.totalCount"
	        },
	        prmNames : {
	            page:"page", 
	            rows:"limit", 
	            order: "order"
	        },
	        gridComplete:function(){
	        	//隐藏grid底部滚动条
	        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
	        	 //单选处理  
                if(selectType=='radio'){  
                	var gridId=grid_selector.substring(1);  
                    $("#cb_"+gridId).hide();//隐藏全选按钮  
                    $(grid_selector).find("td[aria-describedby='"+gridId+"_cb']").find("input[type='checkbox']").prop("type","radio") ;//将checkbox替换为radio  
                    $(grid_selector).find("td[aria-describedby='"+gridId+"_cb']").find("input[type='radio']").prop("name",gridId);//radio设为同一名字   
                }  
	        }
	    };
    $(grid_selector).jqGrid(jqGridSetting);
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		webSite: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

function saveData(obj){
	if(selectType=='radio'){  
		setTemp = new Set();
	}
	setTemp.add(obj+"");
}
function deleteIndexData(obj){
	setTemp.delete(obj+"");
}

//选择多条记录
function getSelectedSets() {
    if(setTemp.size==0){
    	console.info(setTemp.size);
    	alert("请选择一条记录");
    	return ;
    }
    var array = [];
    //setTemp.forEach(v => array.push(v));
    array=Array.from(setTemp);
    return array;
}

//选择多条记录
function getSelectedSetsObject() {
    if(setTemp.size==0){
    	return [];
    }
    var array = [],arraytemp=[];
    array=Array.from(setTemp);
    for (var i = 0; i < array.length; i++) {
		var id=array[i];
    	var itemIdname=commmonSelectSetting.colModel[0]['name'];//数据的主键的名字
    	for (var j = 0; j < setTempobject.length; j++) {
			if(id==setTempobject[j][itemIdname]){//查找数据集中的对应的object对象
				arraytemp.push(setTempobject[j]);
				break;//跳出在数据集中的查找
			}
		}
	}
    
    return arraytemp;
}